# 导入PYMYSQL模块
import pymysql


class MyDB:

    # 链接数据库
    __dba = pymysql.connect(host="localhost", user="root", password="", db="studb", charset="utf8")

    def querysql(self, sql):
        try:
            # dba = pymysql.connect(host="localhost", user="root", password="", db="studb", charset="utf8")
            cursor = self.__dba.cursor()
            cursor.execute(sql)
            data = cursor.fetchall()
            return data
        except Exception as err:

            print("执行失败！", err)

    def executesql(self, sql):
        try:

            cursor = self.__dba.cursor()
            cursor.execute(sql)
            self.__dba.commit()
            rownum = cursor.rowcount()
            return rownum
        except Exception as err:
            self.__dba.rollback()
            print("执行失败！", err)

    def closedb(self):

        self.__dba.close()
